<template>
	<view class="goods-detail-page">
		<!-- 轮播图 -->
		<swiper class="swiper" :indicator-dots="true" :autoplay="true" :circular="true">
			<swiper-item v-for="(item,index) in goodsInfo.pics" :key="index">
				<image :src="item.pics_big" @click="preview(index)"></image>
			</swiper-item>
		</swiper>


		<!-- 商品信息 -->
		<view class="goods-info" v-if="goodsInfo.goods_name">
			<view class="goods-price">
				{{ goodsInfo.goods_price }}
			</view>
			<view class="goods-body">
				<view class="goods-title">
					{{ goodsInfo.goods_name }}
				</view>
				<view class="favi">
					<view class="favi-icon">
						<uni-icons type="star" size="28" color="gray"></uni-icons>
					</view>
					<view class="favi-text">
						收藏
					</view>
				</view>
			</view>
			<view class="express-fee">
				快递： 免运费
			</view>
		</view>


		<!-- 商品介绍 -->
		<rich-text :nodes="goodsInfo.goods_introduce"></rich-text>


		<!-- 商品导航组件 -->
		<view class="goods-nav">
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick" @buttonClick="buttonClick"/>
		</view>
	</view>
</template>

<script>
	import {
		getGoodsDetailAPI
	} from '../../service/goods-detail-api.js'
	
	import {  mapState,mapMutations,mapGetters  } from 'vuex'
	
	
	export default {
		data() {
			return {
				goodsInfo: {},
				options: [{
					icon: 'shop',
					text: '店铺'
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				// 右侧按钮组的配置对象
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]

			}
		},
		methods: {
			...mapMutations('cart',['addToCart']),
			async getGoodsDetail(goodsId) {
				const res = await getGoodsDetailAPI(goodsId)
				if (res.meta.status === 200) {
					res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g,
						'<img style="display:block"').replace(/webp/g, 'jpg')
					this.goodsInfo = res.message
				} else {
					uni.$showMsg()
				}
			},
			preview(index) {
				uni.previewImage({
					current: index,
					urls: this.goodsInfo.pics.map(v => v.pics_big)
				})
			},
			onClick(e) {
				if (e.index === 1) {
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				}
			},
			buttonClick(e) {
				if (e.index === 0) {
					const goods = {
						goods_count: 1,
						goods_id: this.goodsInfo.goods_id,
						goods_name: this.goodsInfo.goods_name,
						goods_price: this.goodsInfo.goods_price,
						goods_small_logo: this.goodsInfo.goods_small_logo,
						goods_state: this.goodsInfo.goods_state,
					}
					this.addToCart(goods)
				}
			}
		},
		onLoad(option) {
			const goodsId = option.goods_id
			this.getGoodsDetail(goodsId)
		},
		computed: {
			...mapState('cart',['cart']),
			...mapGetters('cart',['species'])
		},
		watch: {
			species: {
				immediate: true,
				handler(newValue) {
					this.options[1].info = newValue
				}
			}
		}
	}
</script>

<style lang="scss">
	.goods-detail-page {
		padding-bottom: 50px;
	}

	.swiper {
		height: 476rpx;

		image {
			width: 100%;
			height: 100%;

		}
	}


	.goods-info {
		margin: 32rpx 0 0 32rpx;

		.goods-price {
			font-size: 44rpx;
			color: #E4393C;
		}

		.goods-body {
			margin-top: 12rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.goods-title {
				font-size: 26rpx;
				padding-right: 24rpx;
			}

			.favi {
				width: 120px;
				display: flex;
				border-left: 2px solid #efefef;
				flex-direction: column;
				font-size: 24rpx;
				align-items: center;
				color: gray;
			}
		}

		.express-fee {
			margin: 10px 0;
			font-size: 26rpx;
			color: gray;
		}
	}


	.goods-nav {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		z-index: 9999;
	}
</style>
